package com.itheima.table02;

import com.itheima.bean.Card;
import com.itheima.bean.Classes;
import com.itheima.bean.Student;
import com.itheima.table01.OneToOneMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test02 {

    @Test
    public void selectAll() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);

        List<Classes> classes = mapper.selectAll();
        for (Classes cls: classes) {
            System.out.println(cls.getId()+","+cls.getName());
            List<Student> students = cls.getStudents();
            for (Student student : students) {
                System.out.println("\t"+student);
            }
        }
        is.close();
        sqlSession.close();

    }
}
